package com.tensquare.search.service;

import com.tensquare.search.dao.ArticleSearchDao;
import com.tensquare.search.pojo.Article;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import util.IdWorker;

@Service
public class ArticleSearchService {
    @Autowired
    private ArticleSearchDao articleSearchDao;
    @Autowired
    private IdWorker idWorker;
    //1.添加数据
    public void add(Article article) {
        //1.1 生成id
        article.setId(idWorker.nextId() + "");
        //2.添加数据
        articleSearchDao.save(article);
    }
    //2. 关键字查询
    public PageResult<Article> search(String keywords, int page, int size) {

        Page<Article> page1 = articleSearchDao.findByTitleOrContentLike(keywords,keywords,PageRequest.of(page-1,size));
        return new PageResult(page1.getTotalElements(),page1.getContent());
    }
}
